<?php

include_once 'db.php';

class Bill extends Db {

    public $table_name = "bill";
    public function selectALL($params) {
        $query = 'SELECT * FROM '.$this->table_name.' ORDER BY date DESC';//SQL query 
        if (!empty($params['offset'])) {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' . $offset . ', ' . PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
    public function selectid($id) {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE id = '.$id;//SQL query 
        $data = parent::select($query);
        return $data;
    }
    public function countbillALL() {
        $sql = "SELECT COUNT(*) AS COUNT FROM {$this->table_name}";
        $data = parent::select($sql);
        if (!empty($data[0]['COUNT'])) return (int)$data[0]['COUNT'];
        return 0;
    }
    public function selectSTT($params) {
        /**
         * TODO: Write your code here
         */
        
        $query = 'SELECT * FROM '.$this->table_name.' WHERE status = 0 ORDER BY date DESC';//SQL query 
        if (!empty($params['offset'])) {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' . $offset . ', ' . PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
     public function countbillSTT() {
        $sql = "SELECT COUNT(*) AS COUNT FROM {$this->table_name} WHERE status = 0";
        $data = parent::select($sql);
        if (!empty($data[0]['COUNT'])) return (int)$data[0]['COUNT'];
        return 0;
    }
    public function __construct() {
        parent::__construct();
    }

    public function selectMAXID() {
        /**
         * TODO: Write your code here
         */
        $query = "SELECT MAX(ID) AS ID FROM {$this->table_name}";//SQL query
        $data = parent::select($query);
        return $data;
    }
       
    public function insert($user,$cost) {
        /**
         * TODO: Write your code here
         */
        $date=date("Y-m-d");
        $query = "INSERT INTO bill (id,user,cost,date,status) VALUES('', '".$user."', '".$cost."', '".$date."', '0')";
        $data = parent::insert($query);
        return $data;
    }
    public function update($id) {
        /**
         * TODO: Write your code here
         */
        $query = 'UPDATE '.$this->table_name.' SET status = 1 WHERE id = '.$id;//SQL query
        $data = parent::update($query);
        return $data;
    }
    public function updatecost($cost, $id) {
        /**
         * TODO: Write your code here
         */
        $query = 'UPDATE '.$this->table_name.' SET cost = '.$cost.' WHERE id = '.$id;//SQL query
        $data = parent::update($query);
        return $data;
    }
    public function delete($id) {
        /**
         * TODO: Write your code here
         */
        $query = 'DELETE FROM '.$this->table_name.' WHERE id = '.$id;//SQL query
        $data = parent::delete($query);
        return $data;
    }
    public function count()
    {
        $query = "SELECT COUNT(*) AS COUNT FROM {$this->table_name}";
        $data = parent::select($query);
        return (int) $data[0]['COUNT'];
    }

}
